#include<bits/stdc++.h>

using namespace std;
using ll = long long;
#define endl '\n'

template<typename T=int>
inline T read() {
    T x;
    cin >> x;
    return x;
}

void print(vector<int> a) {
    copy(a.begin(), a.end(), ostream_iterator<int>(cout, " "));
    cout << endl;
}

int main() {
    for (int n; cin >> n;) {
        vector<int> a(n);
        for (auto &e:a)cin >> e;
        vector<int> b(a), c(a);
        prev_permutation(b.begin(), b.end());
        next_permutation(c.begin(), c.end());
        if (b < a) print(b);
        print(a);
        if (c > a) print(c);


    }


    return 0;
}
